package Operation;

import book.Book;
import book.Booklist;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-07-04
 * Time: 22:32
 */
public class DelOperation implements Ioperation {
    @Override
    public void work(Booklist booklist) {
        System.out.println("删除一本书");
        System.out.println("请输入你要删除的书名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentsize=booklist.getUsedsize();
        int index = -1;
        int i = 0;
        for (; i < currentsize; i++) {
            if(booklist.getBook(i).getName().equals(name)){
                index = i;
                break;
            }

        }
        //1.找到了书籍,将书籍的下标保存在index里

      if(index != -1){
          for ( i = index; i < currentsize-1; i++) {
              Book book =booklist.getBook(i+1);
              booklist.setBook(i,book);

          }
          booklist.setBook(currentsize-1,null);
          System.out.println("删除成功");
          booklist.setUsedsize(currentsize-1);
          return;
      }

        //2.i>=currentsize 没哟找到这本书
        if(i>=currentsize){
            System.out.println("找不到这本书");
        }
    }
}
